import Vue from 'vue';
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		cartGoods: []
	},
	mutations: {
		addCart(state, payload) {
			state.cartGoods.push(payload)
			console.log(payload)
		}
	},
	actions: {
		addCartAction({state, commit}, payload) {
			const oldMotion = state.cartGoods.find(item => item.id === payload.id);
			
			if(oldMotion) {
				uni.showToast({
					icon: 'error',
					title: "该运动已经添加",
					duration: 1000
				})
			} else {
				payload.sortNum = 1;
				commit("addCart", payload);
				uni.showToast({
					icon: 'success',
					title: "添加成功",
					duration: 1000,
					success: () => {
						payload ++;
					}
				})
			}
		}
	},
	getters: {},
	modules: {}
});